package converter;

import dao.tasktype.*;
import entity.tasktype.TaskType;

import javax.annotation.ManagedBean;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.springframework.stereotype.Component;

import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;

@FacesConverter(value="taskTypeConverter", forClass = entity.tasktype.TaskType.class)
public class TaskTypeConverter implements javax.faces.convert.Converter {
 
/*	@EJB
	public static TaskTypeDAO ttd;*/
 
	@Inject
	TaskTypeDAO ttd;

	
	public TaskTypeConverter() {
	}
 
	@Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
		long id = 18; 
		/*  try {
	            id = Long.parseLong(value);
		  } catch (NumberFormatException exception) {
	            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, 
	            		"Type the name of a Course and select it (or use the dropdown)", 
	                "Type the name of a Course and select it (or use the dropdown)"));
	        }*/
		TaskType tt = ttd.findById(id, false);
		return tt;
        //return ttd.findById(Long.valueOf(value), true);
    }
	
 
	 @Override
	 public String getAsString(FacesContext context, UIComponent component, Object value) {
	     TaskType tt = null;
	     if(value instanceof TaskType){
	    	 tt = (TaskType)value;
	    	 String ret = tt.getId().toString(); //tt.toString();
	    	 return ret;
	     }
	     return "";
		 //return ((TaskType) value).getId().toString();
	 }
	

}